package models

import "github.com/beego/beego/v2/client/orm"

type WechatFollows struct {
	Id           int64
	DepartmentId int64
	Openid       string
	NickName     string
	Sex          string
	Headimg      string
	FollowsDate  int64
	Address      string
	UpdatedAt    int64
	CreatedAt    int64
}

func WechatFollowsTableName() string {
	return TableName("wechat_follows")
}

func (a *WechatFollows) TableName() string {
	return WechatFollowsTableName()
}

type WechatFollowsQueryParam struct {
	BaseQueryParam
	DepartmentId int64
	Filter       string
}

func WechatFollowsList(param *WechatFollowsQueryParam) []*WechatFollows {
	query := orm.NewOrm().QueryTable(WechatFollowsTableName())
	data := make([]*WechatFollows, 0)
	query.Filter("DepartmentId", param.DepartmentId).OrderBy("-updated_at", "-id").Limit(param.Limit, param.Offset).All(&data)
	return data
}

func WechatFollowsPageList(params *WechatFollowsQueryParam) ([]*WechatFollows, int64) {
	query := orm.NewOrm().QueryTable(WechatFollowsTableName())

	data := make([]*WechatFollows, 0)

	sortorder := "-Id"

	switch params.Sort {
	case "Id":
		sortorder = "Id"
	}
	if params.Order == "desc" {
		sortorder = "-" + sortorder
	}
	cond := orm.NewCondition()

	if params.Filter != "" {
		cond = cond.And("NickName__contains", params.Filter)
	}
	cond = cond.And("DepartmentId", params.DepartmentId)

	query = query.SetCond(cond)

	total, _ := query.Count()

	query.OrderBy(sortorder).Limit(params.Limit, params.Offset).All(&data)

	return data, total
}
